const http = require('http')
const qs=require('querystring')
const config=require('../config')
const apiaddress=require('./apiaddress')

const CoinAdress= async (ctx,next) => {
    const userId =  ctx.session.userId
    if(userId){
        let {coin,address}=ctx.request.body
        let params={address:address}
        let postdata=qs.stringify(params)
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.hostname,
                port: config.port,
                path:`/coin/withdrawAddress/${userId}/${coin}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
    }
}
////////////////////////提现地址
const CashAdress= async (ctx,next) => {
    const userId =  ctx.session.userId
    if(userId){
        let {coin,a,addr}=ctx.request.body
        let params={a:a,addr:addr}
        let postdata=qs.stringify(params)
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.hostname,
                port: config.port,
                path:`/coin/withdraw/${userId}/${coin}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }
}

const deposit= async (ctx,next) => {
    const userId =  ctx.session.userId
    if(userId){
        let {coin}=ctx.request.body
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.hostname,
                port: config.port,
                path:`/coin/depositAddress/${userId}/${coin}`,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            // req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }else{
    }
}

////////////////////////转账地址
const userpublic= async (ctx,next) => {
    const userId =  ctx.session.userId
    if(userId){
        let {coin,a,u,e}=ctx.request.body
        let params={a:a,u:u,e:e}
        let postdata=qs.stringify(params)
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.hostname,
                port: config.port,
                path:`/coin/transfer/${userId}/${coin}`,
                method: "POST",
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': postdata.length
                }
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.write(postdata)
            req.end()
        })
        let data=await result
        if(data.code&&data.code!=200){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }
}

//免费领币接口
const freeFaucet= async (ctx,next) => {
    const userId = ctx.session.userId
    if(userId){
        let {coin}=ctx.request.body
        const result=new Promise((resolve,reject) => {
            let opt={
                hostname:config.hostname,
                port: config.port,
                path:`/coin/faucet/${userId}`,
                method: "PUT"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    resolve(JSON.parse(resdata))
                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.error(data)
        }else{
            ctx.success(data)
        }
    }
}

//发送邮件
const sendCode = async (ctx, next) => {
    let {type,sendTo} = ctx.request.body;
    let params = {sendTo: sendTo,type:type };
    let postdata = qs.stringify(params);
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.mailhost,
            port: config.mailport,
            path: `${apiaddress.sendCode}${type}`,
            method: "POST",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Content-Length": postdata.length
            }
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
                resdata += chunk;
            });
            res.on("end", () => {
                resolve(JSON.parse(resdata));
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.write(postdata);
        req.end();
    });
    let data = await result;
    if (data.code) {
        ctx.error(data);
    } else {
        ctx.success(data);
    }
}

//重置密码
const sendCodetwo = async (ctx, next) => {
    let {email,password,checkCode} = ctx.request.body;
    // let {email,password} = ctx.request.body;
    let params = {email: email, password,password,checkCode,checkCode};
    // let params = {email, password};
    let postdata = qs.stringify(params);
    const result = new Promise((resolve, reject) => {
        let opt = {
            hostname: config.hostname,
            port: config.port,
            path: `/user/forgetPassword`,
            method: "POST",
            headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-Length": postdata.length
            }
        };
        let req = http.request(opt, res => {
            res.setEncoding("utf8");
            let resdata = "";
            res.on("data", chunk => {
            resdata += chunk;
            });
            res.on("end", () => {
            resolve(JSON.parse(resdata));
            });
        });
        req.on("error", e => {
            reject(e);
        });
        req.write(postdata);
        req.end();
    });
    let data = await result;
    if (data.code) {
        ctx.error(data);
    } else {
        ctx.success(data);
    }
};

module.exports={
    CoinAdress,
    CashAdress,
    deposit,
    userpublic,
    freeFaucet,
    sendCode,
    sendCodetwo
}